﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using mtgtoolbox.Controls.CollectionList.ViewModels;
using System.ComponentModel;
using mtgtoolbox.Models;
using mtgtoolbox.Support;
using Castle.Windsor;
using Castle.MicroKernel;

namespace mtgtoolbox.Controls.CollectionList
{
  /// <summary>
  /// Interaction logic for CollectionList.xaml
  /// </summary>
  public partial class CollectionListControl : UserControl
  {
    private ICollectionView filterView;

    ItemsControl ItemSource
    {
      get;
      set;
    }

    public void LoadCollection(String collectionName)
    {
      collectionListViewModel.LoadCollection(collectionName);
    }
    
    private Card selectedCard = null;
    public Card SelectedCard
    {
      get { return selectedCard; }
    }

    public event EventHandler<SelectionChangedEventArgs> SelectionChanged;
    private void listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      CollectionListCardViewModel selectedItem = listbox.SelectedItem as CollectionListCardViewModel;

      if (selectedItem != null)
      {

        selectedCard = (listbox.SelectedItem as CollectionListCardViewModel).Card;
        e.Handled = true;
        if (SelectionChanged != null)
          SelectionChanged(this, e);
      }
    }

    public CollectionListControl()
    {
      InitializeComponent();
      //this.collectionListViewModel = collectionListViewModel;

      // Setup the filterview
      //
      filterView = CollectionViewSource.GetDefaultView(collectionListViewModel.Cards);
      filterView.Filter = delegate(object o)
      {
        if (collectionListViewModel.FilterText == "")
          return true;
        else
          return ((CollectionListCardViewModel)o).Name.ToLower().Contains(collectionListViewModel.FilterText.ToLower());
      };
    }

    private void listBox_KeyUp(object sender, KeyEventArgs e)
    {
      CollectionListCardViewModel cardViewModel = ((DataGrid)sender).SelectedItem as CollectionListCardViewModel;

      if (cardViewModel == null) return;

      List<Key> validKeys = new List<Key>(){ Key.A,Key.B,Key.C,Key.D,Key.E,Key.F,Key.G,Key.H,Key.I,Key.J,Key.K,Key.L,Key.M,Key.N,Key.O
                                            ,Key.P,Key.Q,Key.R,Key.S,Key.T,Key.U,Key.V,Key.W,Key.X,Key.Y,Key.Z };
      //Keyboard.Modifiers;
      if (e.Key == Key.OemPlus)
      {
        cardViewModel.Count++;
        e.Handled = true;
      }
      else if (e.Key == Key.OemMinus)
      {
        if (cardViewModel.Count > 0)
          cardViewModel.Count--;
        e.Handled = true;
      }
      else if (e.Key == Key.Enter)
      {
        if (collectionListViewModel.FilterText != "")
        {
          collectionListViewModel.FilterText = "";
          filterView.Refresh();
          listbox.Focus();
          listbox.SelectedIndex = 0;
        }
        e.Handled = true;
      }
      else if (e.Key == Key.Back)
      {
        if (collectionListViewModel.FilterText.Length > 0)
        {
          collectionListViewModel.FilterText = collectionListViewModel.FilterText.Substring(0, collectionListViewModel.FilterText.Length - 1);
          filterView.Refresh();
        }
        e.Handled = true;
      }
      else if (validKeys.Contains(e.Key))
      {
        KeyConverter k = new KeyConverter();
        collectionListViewModel.FilterText += k.ConvertToString(e.Key);
        filterView.Refresh();
        listbox.Focus();
        listbox.SelectedIndex = 0;
        e.Handled = true;
      }
    }


  }
}
